#include "StdAfx.h"
#include "res_str_mgr.h"
#include "global_data.h"
#include <utility/api_tools.h>
#include <interface/log_interface.h>


struct res_pair_t
{
	int		id;
	const char* key;
	std::string val;
};

static res_pair_t		res_pair_table[] = 
{
	{ aot::glb::IDS_GROUP_SYS, "IDS_GROUP_SYS", "" },
	{ aot::glb::IDS_GROUP_SERVICE, "IDS_GROUP_SERVICE", "" },
	{ aot::glb::IDS_STATUS_ONLINE, "IDS_STATUS_ONLINE", "" },
	{ aot::glb::IDS_STATUS_HIDE, "IDS_STATUS_HIDE", "" },
	{ aot::glb::IDS_STATUS_BUSY, "IDS_STATUS_BUSY", "" },
	{ aot::glb::IDS_STATUS_AWAY, "IDS_STATUS_AWAY", "" },
	{ aot::glb::IDS_STATUS_OFFLINE, "IDS_STATUS_OFFLINE", "" },
	{ aot::glb::IDS_CALL_ME, "IDS_CALL_ME", "" },
	{ aot::glb::IDS_CALL_LINKMAN, "IDS_CALL_LINKMAN", "" },
	{ aot::glb::IDS_CONNECTING, "IDS_CONNECTING", "" },
	{ aot::glb::IDS_LINKMAN_BUSY, "IDS_LINKMAN_BUSY", "" },
	{ aot::glb::IDS_KEEP_CALLING, "IDS_KEEP_CALLING", "" },
	{ aot::glb::IDS_NEW_MESSAGE, "IDS_NEW_MESSAGE", "" },
	{ aot::glb::IDS_LINKMAN_ONLINE, "IDS_LINKMAN_ONLINE", "" },
};

const char* get_res_string(int id)
{
	return g_dll_obj->res_string_mgr_->get_string(id);
}

global_res_string_mgr::global_res_string_mgr(void)
{
}

global_res_string_mgr::~global_res_string_mgr(void)
{
}

bool 
global_res_string_mgr::reloading_res(const char* language/* = 2052*/)
{
    std::string app_fn = aot::tt::utility::get_app_path();
    std::string app_root_path;
    const char* pos = 
        aot::tt::utility::str_find_lastof(app_fn.c_str(), "\\/", (int)app_fn.size()-1);
    if( pos )
        app_root_path = app_fn.substr(0, pos - app_fn.c_str() + 1);
    else
        app_root_path = app_fn;
    std::string fn = app_root_path;
    fn += "language/";
    fn += language;
    fn += "/global_data_string.xml"; 

	aot::tt::res_parser_string_helper	res_parser;

	if( !g_dll_obj->main_obj->query_interface(res_parser, "ires_parser") )
	{
		aot_log_error(AOT_LM_ERROR, "%s", "ires_parser init failed !");
		return false;
	}

	if( !res_parser.open(fn.c_str()) )
	{
		aot_log_error(AOT_LM_ERROR, "open file failed: %s", fn.c_str());
		return false;
	}

	for( int i = 0; i < sizeof(res_pair_table)/sizeof(res_pair_table[0]); ++i)
	{
		res_pair_table[i].val = res_parser.get_string(res_pair_table[i].key, "");

		if(res_pair_table[i].val.length() < 1)
		{
			aot_log_error(AOT_LM_ERROR, "loading res [%s] failed", res_pair_table[i].key);
		}
	}

	return true;
}

const char* 
global_res_string_mgr::get_string(int id)
{
	assert( id >= 0 && id < sizeof(res_pair_table)/sizeof(res_pair_table[0]) );

	if( id < 0 && id >= sizeof(res_pair_table)/sizeof(res_pair_table[0]) )
		return "";

	return res_pair_table[id].val.c_str();
}